<?php
namespace app\user\model;
use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;
    protected $globalScope = ['shop_id'];
    
    public function scopeShop_id($query)
    {
        $query->where('shop_id', ShopId());
    }
    public function setPasswordAttr($value)
    {
        return md5($value);
    }

    public function userGroup()
    {
        return $this->belongsTo(UserGroup::class, 'group_id', 'id')->bind(['rules','group_name'=>'name']);
    }
    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }
    public function getLastLoginTimeAttr($value) {
        return date('Y-m-d H:i:s',$value);
    }
    public function setRuleIdAttr($value)
    {
        if(!$value){
            $list = array();
        }else{
            $list = implode(',',$value);
        }
        return $list;
    }
    public function getRuleIdAttr($value)
    {
        if(!$value){
            $list = array();
        }else{
            $list = explode(',', $value);
        }
        return $list;
    }
    //获取列表
    static function lists() {
        return self::where('status','1')->order('sort_order desc,id desc')->select();
    }
}